package com.data4.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data4.feignapi.common.constants.UserConstants;
import com.data4.feignapi.common.tools.RegexUtils;
import com.data4.feignapi.config.JwtUtils;
import com.data4.feignapi.dto.PhonePwdLoginDTO;
import com.data4.feignapi.dto.Result;
import com.data4.feignapi.dto.UserDTO;
import com.data4.feignapi.entity.DUser;
import com.data4.feignapi.vo.UserVO;
import com.data4.user.mapper.DUserMapper;
import com.data4.user.service.DUserService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.impl.DefaultClaims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Forever
 * @since 2024-12-11
 */
@Service
public class DUserServiceImpl extends ServiceImpl<DUserMapper, DUser> implements DUserService {

    @Resource
    private JwtUtils jwtUtils;

    @Override
    public Result register(UserDTO userDTO) {
        if (exists(new LambdaQueryWrapper<DUser>()
                .eq(DUser::getPhone, userDTO.getPhone()))) {
            return Result.fail(UserConstants.PHONE_EXIST);
        }
        boolean isPhone = RegexUtils.isPhoneInvalid(userDTO.getPhone());
        boolean isPWD = RegexUtils.isPasswordInvalid(userDTO.getPwd());
        if (!isPhone) {
            return Result.fail(UserConstants.PHONE_NOT_TRUE);
        }
        if (!isPWD) {
            return Result.fail(UserConstants.PWD_NOT_TRUE);
        }
        DUser user = BeanUtil.toBean(userDTO, DUser.class);
        save(user);
        return Result.ok(UserConstants.REGISTER_SUCCESS);
    }

    @Override
    public Result login(PhonePwdLoginDTO pwdLoginDTO) {
        if (!exists(new LambdaQueryWrapper<DUser>()
                .eq(DUser::getPhone, pwdLoginDTO.getPhone()))) {
            return Result.fail(UserConstants.NOT_REGISTER);
        }
        DUser user = lambdaQuery()
                .eq(DUser::getPhone, pwdLoginDTO.getPhone())
                .eq(DUser::getPwd, pwdLoginDTO.getPwd())
                .select(DUser::getUserId, DUser::getUserName)
                .one();
        if (BeanUtil.isEmpty(user)) {
            return Result.fail(UserConstants.LOGIN_FAIL);
        }
        UserVO userVO = BeanUtil.toBean(user, UserVO.class);
        String token = UserTWTToken(userVO);
        return Result.ok(token);
    }

    @Override
    public Result getUserInfoById(Integer userId) {
        DUser user = lambdaQuery()
                .eq(DUser::getUserId, userId)
                .select(DUser::getUserId, DUser::getUserName,
                        DUser::getPhone, DUser::getAddress)
                .one();
        UserVO userVO = BeanUtil.toBean(user, UserVO.class);
        return Result.ok(userVO);
    }

    private String UserTWTToken(UserVO userVO) {
        //创造负载体
        Claims claims = new DefaultClaims();
        //放入用户信息
        claims.put(UserConstants.USER_TOKEN_PREFIX, userVO);
        //生成token
        String token = jwtUtils.generateToken(claims);
        //设置token
       return token;
    }
}
